package com.ruoyi.project.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.literature.domain.Literature;
import com.ruoyi.literature.mapper.LiteratureMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mapper.ProjectMapper;
import com.ruoyi.project.domain.Project;
import com.ruoyi.project.service.IProjectService;

/**
 * 项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-02-24
 */
@Service
public class ProjectServiceImpl implements IProjectService 
{
    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    LiteratureMapper literatureMapper;

    /**
     * 查询项目
     * 
     * @param projectId 项目主键
     * @return 项目
     */
    @Override
    public Project selectProjectByProjectId(Long projectId)
    {
        return projectMapper.selectProjectByProjectId(projectId);
    }

    /**
     * 查询项目列表
     * 
     * @param project 项目
     * @return 项目
     */
    @Override
    public List<Project> selectProjectList(Project project)
    {
        return projectMapper.selectProjectList(project);
    }

    /**
     * 新增项目
     * 
     * @param project 项目
     * @return 结果
     */
    @Override
    public int insertProject(Project project)
    {
        project.setCreateTime(DateUtils.getNowDate());
        return projectMapper.insertProject(project);
    }

    /**
     * 修改项目
     * 
     * @param project 项目
     * @return 结果
     */
    @Override
    public int updateProject(Project project)
    {
        return projectMapper.updateProject(project);
    }

    /**
     * 批量删除项目
     * 
     * @param projectIds 需要删除的项目主键
     * @return 结果
     */
    @Override
    public int deleteProjectByProjectIds(Long[] projectIds)
    {
        return projectMapper.deleteProjectByProjectIds(projectIds);
    }

    /**
     * 删除项目信息
     * 
     * @param projectId 项目主键
     * @return 结果
     */
    @Override
    public int deleteProjectByProjectId(Long projectId)
    {
        return projectMapper.deleteProjectByProjectId(projectId);
    }

    /**
     * 导入文献到项目
     * */
    @Override
    public int importLiterature(Long projectId, Long[] literatureList) {
        projectMapper.importLiterature(projectId, literatureList);

        return 0;
    }

    @Override
    public void removeLiterature(Long projectId, Long[] literatureIds) {
        projectMapper.removeLiterature(projectId, literatureIds);
    }

    /**
     * 查看项目中包含的文献*/
    @Override
    public TableDataInfo selectLiteratureByProjectId(Long projectId) {
        //1.先查询项目中的文献ID
        List<Long> literatureIds=projectMapper.selectLiteratureByProjectId(projectId);

        //2.再根据文献ID查询文献信息
        List<Literature> literatureList = new ArrayList<>();
        if(literatureIds!=null && literatureIds.size()>0){
            for(Long id : literatureIds){

                Literature literature = literatureMapper.selectLiteratureByLiteratureId(id);
                literatureList.add(literature);
            }
        }
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(literatureList);
        tableDataInfo.setTotal(new PageInfo(literatureIds).getTotal());
        return tableDataInfo;
    }


}
